<%-
	require "luci.sys"
	require "luci.tools.webadmin"
	require "nixio.fs"
	require "luci.controller.idm"

	local style = true
-%>

<%+header%>

<form name="idm" method="post" action="<%=REQUEST_URI%>">
	<div>
		<input type="hidden" id="act" name="act" value="none" />
		<input type="hidden" id="obj" name="obj" value="<%=obj%>" />
	</div>
	<div class="cbi-map" id="cbi-group"><fieldset class="cbi-section" id="cbi-table-table">
		<table class="cbi-section-table">
			<tr class="cbi-section-table-titles">
				<th class="cbi-section-table-cell left" style="width: 15%"><%:Group%></th>
				<th class="cbi-section-table-cell left" style="width: 40%"><%:Description%></th>
				<th class="cbi-section-table-cell left" style="width: 15%"><%:GID%></th>
				<th class="cbi-section-table-cell left" style="width: 30%"></th>
			</tr>
			<% style = nil; newgid = 103; for gid,grp in pairs(groups) do %>
			<tr class="cbi-section-table-row cbi-rowstyle-<%=(style and 1 or 2)%>" rowspan="2">
				<td class="cbi-section-table-cell left">
					<div style="text-indent: 17px; background: url('<%=resource%>/cbi/group.gif') no-repeat scroll 1px center;"><%=grp.cn%></div>
				</td>
				<% if (act == "none") or not (obj == grp.dn) then %>
				<td class="cbi-section-table-cell left"><%=grp.desc%></td>
				<td class="cbi-section-table-cell left"><%=gid%></td>
				<% else %>
				<td class="cbi-section-table-cell left"><input class="cbi-input-text" type="text" name="desc" value="<%=grp.desc%>" /></td>
				<td class="cbi-section-table-cell left"><input class="cbi-input-text" type="text" name="gid"  value="<%=gid%>"  /></td>
				<% end %>
				<td class="cbi-section-table-cell right">
					<% if act == "none" then %>
					<input type="image" src="<%=resource%>/cbi/edit.gif" 
						onclick="document.getElementById('act').value='grpmod';document.getElementById('obj').value='<%=grp.dn%>'" />
					<input type="image" src="<%=resource%>/cbi/remove.gif"
						onclick="ret=window.confirm('<%:Are you sure?%>');if(ret){document.getElementById('act').value='grprm';document.getElementById('obj').value='<%=grp.dn%>'};return ret" />
					<% end %>
				</td>
			</tr><tr style="height:0px" />
			<% style = not style; if newgid <= gid then newgid = gid + 1 end; end %>
			<% if act == "grpadd" then %>
			<tr class="cbi-section-table-row" rowspan="2">
				<td class="cbi-section-table-cell left">
					<input class="cbi-input-user" type="text" name="cn" value="" style="background-image: url('<%=resource%>/cbi/group.gif'; background-repeat:none)" />
				</td>
				<td class="cbi-section-table-cell left"><input class="cbi-input-text" type="text" name="desc" value="" /></td>
				<td class="cbi-section-table-cell left"><input class="cbi-input-text" type="text" name="gid"  value="<%=newgid%>"  /></td>
			</tr><tr style="height:0px" />
			<% end %>
			<% if act == "grpadd" or act == "grpmod" then %>
			<tr class="cbi-section-table-row" rowspan="2">
				<td colspan="5" class="cbi-section-table-cell left">
					<input class="cbi-button cbi-button-save" type="submit" value="<%:Save%>"
						onclick="document.getElementById('act').value='<%=act%>ok'" />
					<input class="cbi-button cbi-button-reset" type="submit" value="<%:Cancel%>"
						onclick="document.getElementById('act').value='none'" />
				</td>
			</tr><tr style="height:0px" />
			<% end %>
			<% if act == "none" then %>
			<tr class="cbi-section-table-row" rowspan="2">
				<td colspan="5" class="cbi-section-table-cell left">
					<input class="cbi-button cbi-button-add" type="submit" value="<%:Add%>"
						onclick="document.getElementById('act').value='grpadd'" />
				</td>
			</tr><tr style="height:0px" />
			<% end %>
		</table></fieldset>
	</div>
	<div class="cbi-map" id="cbi-user"><fieldset class="cbi-section" id="cbi-table-table">
		<table class="cbi-section-table">
			<tr class="cbi-section-table-titles">
				<th class="cbi-section-table-cell left" style="width: 15%"><%:User%></th>
				<th class="cbi-section-table-cell left" style="width: 15%"><%:Given name%></th>
				<th class="cbi-section-table-cell left" style="width: 15%"><%:Last name%></th>
				<th class="cbi-section-table-cell left" style="width: 10%"><%:UID%></th>
				<th class="cbi-section-table-cell left" style="width: 15%"><%:Group%></th>
				<th class="cbi-section-table-cell left" style="width: 20%"><%:Kerberos principal%></th>
				<th class="cbi-section-table-cell" style="width: 10%"></th>
			</tr>
			<% style = nil; newuid = 1000; newgid = 100; for uid,usr in pairs(users) do %>
			<tr class="cbi-section-table-row cbi-rowstyle-<%=(style and 1 or 2)%>" rowspan="2">
				<td class="cbi-section-table-cell left">
					<div style="text-indent: 17px;background: url('<%=resource%>/cbi/user.gif') no-repeat scroll 1px center;"><%=uid%></div>
				</td>
				<%if (act == "none") or not (obj == usr.dn) then%>
				<td class="cbi-section-table-cell left"><%=usr.gn%></td>
				<td class="cbi-section-table-cell left"><%=usr.sn%></td>
				<td class="cbi-section-table-cell left"><%=usr.uidn%></td>
				<td class="cbi-section-table-cell left"><%=grp_lbl[usr.gid] or usr.gid%></td>
				<td class="cbi-section-table-cell left"><%=usr.kpn%></td>
				<td class="cbi-section-table-cell right">
					<%if act == "none" then%>
					<input type="image" src="<%=resource%>/cbi/edit.gif" 
						onclick="document.getElementById('act').value='usrmod';document.getElementById('obj').value='<%=usr.dn%>'" />
					<input type="image" src="<%=resource%>/cbi/remove.gif"
						onclick="ret=window.confirm('<%:Are you sure?%>');if(ret){document.getElementById('act').value='usrrm';document.getElementById('obj').value='<%=usr.dn%>'};return ret" />
					<%end%>
				</td>
				<%else%>
				<td class="cbi-section-table-cell left"><input class="cbi-input-text" type="text" name="gn" value="<%=usr.gn%>" /></td>
				<td class="cbi-section-table-cell left"><input class="cbi-input-text" type="text" name="sn" value="<%=usr.sn%>" /></td>
				<td class="cbi-section-table-cell left"><input class="cbi-input-text" type="text" name="uidn" value="<%=usr.uidn%>" /></td>
				<td class="cbi-section-table-cell left"><select class="cbi-input-select" name="gid">"
				<%for gid,label in pairs(grp_lbl) do%>
					<%if usr.gid == gid then selected=true %>
						<option selected="selected" value="<%=gid%>"><%=label%></option>
					<%else%>
						<option value="<%=gid%>"><%=label%></option>
					<%end%>
				<%end%>
				<%if not selected then%>
					<option selected="selected" value="<%=usr.gid%>"><%=usr.gid%></option>
				<%end%>
				</select></td>
				<td class="cbi-section-table-cell left">
					<%if type(usr.kpn) then%><input class="cbi-input-text" type="text" name="kpn" value="<%=usr.kpn%>" /><%else%><%=usr.kpn%><%end%>
				</td>
				<td class="cbi-section-table-cell"></td>
			</tr><tr style="height:0px" />
			<tr class="cbi-section-table-row cbi-rowstyle-<%=(style and 1 or 2)%>" rowspan="2">
				<td class="cbi-section-table-cell right"><%:Password%></td>
				<td class="cbi-section-table-cell left"><input class="cbi-input-password" type="password" name="pw" value="" /></td>
				<td class="cbi-section-table-cell right"><%:Home dir%></td>
				<td class="cbi-section-table-cell left"><input class="cbi-input-text" type="text" name="homedir" value="<%=usr.homedir%>" /></td>
				<td class="cbi-section-table-cell right"><%:Shell%></td>
				<td class="cbi-section-table-cell left"><input class="cbi-input-text" type="text" name="shell" value="<%=usr.shell%>" /></td>
				<% end %>
			</tr><tr style="height:0px" />
			<% style = not style; if newuid <= usr.uidn then newuid = usr.uidn + 1 end; newgid = usr.gid; end %>


			<% if act == "usradd" then %>
			<tr class="cbi-section-table-row" rowspan="2">
				<td class="cbi-section-table-cell left"><input class="cbi-input-user" type="text" name="uid" value="" /></td>
				<td class="cbi-section-table-cell left"><input class="cbi-input-text" type="text" name="gn" value="" /></td>
				<td class="cbi-section-table-cell left"><input class="cbi-input-text" type="text" name="sn" value="" /></td>
				<td class="cbi-section-table-cell left"><input class="cbi-input-text" type="text" name="uidn" value="<%=newuid%>" /></td>
				<td class="cbi-section-table-cell left"><select class="cbi-input-select" name="gid">"
				<%for gid,label in pairs(grp_lbl) do%>
					<%if gid == newgid then selected=true %>
						<option selected="selected" value="<%=gid%>"><%=label%></option>
					<%else%>
						<option value="<%=gid%>"><%=label%></option>
					<%end%>
				<% end %>
				<%if not selected then%>
					<option selected="selected" value="<%=newgid%>"><%=newgid%></option>
				<%end%>
				</select></td>
				<td class="cbi-section-table-cell"></td>
				<td class="cbi-section-table-cell"></td>
			</tr><tr style="height:0px" />
			<tr class="cbi-section-table-row" rowspan="2">
				<td class="cbi-section-table-cell right"><%:Password%></td>
				<td class="cbi-section-table-cell left"><input class="cbi-input-password" type="password" name="pw" value="" /></td>
				<td class="cbi-section-table-cell right"><%:Home dir%></td>
				<td class="cbi-section-table-cell left"><input class="cbi-input-text" type="text" name="homedir" value="/tmp" /></td>
				<td class="cbi-section-table-cell right"><%:Shell%></td>
				<td class="cbi-section-table-cell left"><input class="cbi-input-text" type="text" name="shell" value="/bin/false" /></td>
			</tr><tr style="height:0px" />
			<% end %>


			<% if act == "usradd" or act == "usrmod" then -%>
			<tr class="cbi-section-table-row" rowspan="2">
				<td colspan="7" class="cbi-section-table-cell left">
					<input class="cbi-button cbi-button-save" type="submit" value="<%:Save%>"
						onclick="document.getElementById('act').value='<%=act%>ok';document.getElementById('obj').value='<%=obj%>'" />
					<input class="cbi-button cbi-button-reset" type="submit" value="<%:Cancel%>"
						onclick="document.getElementById('act').value='none'" />
				</td>
			</tr><tr style="height:0px" />
			<% end %>
			<% if act == "none" then %>
			<tr class="cbi-section-table-row" rowspan="2">
				<td colspan="7" class="cbi-section-table-cell left">
					<input class="cbi-button cbi-button-add" type="submit" value="<%:Add%>"
						onclick="document.getElementById('act').value='usradd'" />
				</td>
			</tr><tr style="height:0px" />
			<% end %>
		</table></fieldset>
	</div>
	<div class="cbi-map" id="cbi-host"><fieldset class="cbi-section" id="cbi-table-table">
		<table class="cbi-section-table">
			<tr class="cbi-section-table-titles">
				<th class="cbi-section-table-cell left" style="width: 15%"><%:Computer%></th>
				<th class="cbi-section-table-cell left" style="width: 30%"><%:Description%></th>
				<th class="cbi-section-table-cell left" style="width: 10%"><%:UID%></th>
				<th class="cbi-section-table-cell left" style="width: 15%"><%:Group%></th>
				<th class="cbi-section-table-cell left" style="width: 20%"><%:Kerberos principal%></th>
				<th class="cbi-section-table-cell left" style="width: 10%"></th>
			</tr>
			<% style = nil; newuid = 2000; newgid = 102; for uid,host in pairs(hosts) do %>
			<tr class="cbi-section-table-row cbi-rowstyle-<%=(style and 1 or 2)%>" rowspan="2">
				<td class="cbi-section-table-cell left">
					<div style="text-indent: 17px;background: url('<%=resource%>/cbi/server.gif') no-repeat scroll 1px center;"><%=uid%></div>
				</td>
			   <%if (act == "none") or not (obj == host.dn) then%>
				<td class="cbi-section-table-cell left"><%=host.desc%></td>
				<td class="cbi-section-table-cell left"><%=host.uidn%></td>
				<td class="cbi-section-table-cell left"><%=grp_lbl[host.gid] or host.gid%></td>
				<td class="cbi-section-table-cell left">
					<%if type(host.kpn) == "string" then%><%=host.kpn%><%else%>
						<%first=true; for key,value in pairs (host.kpn) do%>
							<%if not first then%><br/><%end%><%=value%><%first=nil%>
						<%end%>
					<%end%>
				</td>
				<td class="cbi-section-table-cell right">
				   <%if act == "none" then%>
					<input type="image" src="<%=resource%>/cbi/edit.gif" 
						onclick="document.getElementById('act').value='hostmod';document.getElementById('obj').value='<%=host.dn%>'" />
					<input type="image" src="<%=resource%>/cbi/remove.gif"
						onclick="ret=window.confirm('<%:Are you sure?%>');if(ret){document.getElementById('act').value='hostrm';document.getElementById('obj').value='<%=host.dn%>'};return ret" />
				   <%end%>
				</td>
			    <%else%>
				<td class="cbi-section-table-cell left"><input class="cbi-input-text" type="text" name="desc" value="<%=host.desc%>" /></td>
				<td class="cbi-section-table-cell left"><input class="cbi-input-text" type="text" name="uidn" value="<%=newuid%>" /></td>
				<td class="cbi-section-table-cell left"><select class="cbi-input-select" name="gid">"
				   <%for gid,label in pairs(grp_lbl) do%>
				     <%if host.gid == gid then selected=true %>
					<option selected="selected" value="<%=gid%>"><%=label%></option>
				     <%else%>
					<option value="<%=gid%>"><%=label%></option>
				     <%end%>
				   <%end%>
				   <%if not selected then%>
					<option selected="selected" value="<%=host.gid%>"><%=host.gid%></option>
				   <%end%>
				</select></td>
				<%if type(host.kpn) == "string" then%>
				<td class="cbi-section-table-cell left">
					<input class="cbi-input-text" type="text" name="kpn" value="<%=host.kpn%>" />
				</td>
			</tr>
			<tr style="height:0px" />
			<tr class="cbi-section-table-row" rowspan="2">
				<td class="cbi-section-table-cell right"><%:Password%></td>
				<td class="cbi-section-table-cell left"><input class="cbi-input-password" type="password" name="pw" value="" /></td>
				<%else%>
				<td class="cbi-section-table-cell left">
					<%first=true; for key,value in pairs (host.kpn) do%>
						<%if not first then%><br/><%end%><%=value%><%first=nil%>
					<%end%>
				</td>
				<%end%>
				<%end%>
			</tr><tr style="height:0px" />
			<% style = not style; if newuid <= host.uidn then newuid = host.uidn + 1 end; newgid = host.gid; end %>
			<% if act == "hostadd" then %>
			<tr class="cbi-section-table-row" rowspan="2">
				<td class="cbi-section-table-cell left"><input class="cbi-input-user" type="text" name="uid" value="" /></td>
				<td class="cbi-section-table-cell left"><input class="cbi-input-text" type="text" name="desc" value="" /></td>
				<td class="cbi-section-table-cell left"><input class="cbi-input-text" type="text" name="uidn" value="<%=newuid%>" /></td>
				<td class="cbi-section-table-cell left"><select class="cbi-input-select" name="gid">"
				<%for gid,label in pairs(grp_lbl) do%>
					<%if gid == newgid then selected=true %>
						<option selected="selected" value="<%=gid%>"><%=label%></option>
					<%else%>
						<option value="<%=gid%>"><%=label%></option>
					<%end%>
				<% end %>
				<%if not selected then%>
					<option selected="selected" value="<%=newgid%>"><%=newgid%></option>
				<%end%>
				</select></td>
			</tr><tr style="height:0px" />
			<tr class="cbi-section-table-row" rowspan="2">
				<td class="cbi-section-table-cell right"><%:Password%></td>
				<td class="cbi-section-table-cell left"><input class="cbi-input-password" type="password" name="pw" value="" /></td>
			</tr><tr style="height:0px" />
			<% end %>
			<% if act == "hostadd" or act == "hostmod" then -%>
			<tr class="cbi-section-table-row" rowspan="2">
				<td colspan="6" class="cbi-section-table-cell left">
					<input class="cbi-button cbi-button-save" type="submit" value="<%:Save%>"
						onclick="document.getElementById('act').value='<%=act%>ok';document.getElementById('obj').value='<%=obj%>'" />
					<input class="cbi-button cbi-button-reset" type="submit" value="<%:Cancel%>"
						onclick="document.getElementById('act').value='none'" />
				</td>
			</tr><tr style="height:0px" />
			<% elseif act == "none" then %>
			<tr class="cbi-section-table-row" rowspan="2">
				<td colspan="6" class="cbi-section-table-cell left">
					<input class="cbi-button cbi-button-add" type="submit" value="<%:Add%>"
						onclick="document.getElementById('act').value='hostadd'" />
				</td>
			</tr><tr style="height:0px" />
			<% end %>
		</table></fieldset>
	</div>
</form>

<%+footer%>
